import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// import Home from '@/views/Home/Home.vue' 优化阶段用懒加载
import Search from '@/views/Search/index.vue'
import Login from '@/views/Login/index.vue'
import Register from '@/views/Register/index.vue'
import Detail from '@/views/Detail/index.vue'
import AddCartSuccess from '@/views/AddCartSuccess/index.vue'
import ShopCart from '@/views/ShopCart/index.vue'
import Trade from '@/views/Trade/index.vue'
import Pay from '@/views/Pay/index.vue'
import Paysuccess from '@/views/PaySuccess/index.vue'
import Center from '@/views/Center/index.vue'
import Myorder from '@/views/Center/myOrder/index.vue'
import Grouporder from '@/views/Center/groupOrder/index.vue'

// 重写push方法
let orginalPush = VueRouter.prototype.push
VueRouter.prototype.push = function(location, resolve, reject) {
    if (resolve && reject) {
        orginalPush.call(this, location, resolve, reject)
    } else {
        orginalPush.call(this, location, () => {}, () => {})
    }
}

const router = new VueRouter({
    routes: [
        { path: '/', redirect: '/home' },
        { path: '/home', component: () =>
                import ('@/views/Home/Home.vue'), meta: { footIsShow: true } },
        { path: '/search/:keyword?', component: Search, meta: { footIsShow: true }, name: 'search' },
        { path: '/login', component: Login, meta: { footIsShow: false } },
        { path: '/register', component: Register, meta: { footIsShow: false } },
        { path: '/detail/:skuId', component: Detail, meta: { footIsShow: false } },
        {
            path: '/addcartsuccess',
            component: AddCartSuccess,
            name: 'addcartsuccess',
            meta: { footIsShow: true },
            // 路由独享守卫
            beforeEnter: (to, from, next) => {

            }
        },
        { path: '/shopcart', component: ShopCart, name: 'shopcart', meta: { footIsShow: false } },
        {
            path: '/trade',
            component: Trade,
            meta: { footIsShow: true },
            // 路由独享守卫
            beforeEnter: (to, from, next) => {
                if (from.path == '/shopcart') {
                    next()
                } else {
                    next(false)
                }
            }
        },
        {
            path: '/pay',
            component: Pay,
            name: 'pay',
            meta: { footIsShow: true },
            // 路由独享守卫
            beforeEnter: (to, from, next) => {
                if (from.path == '/trade') {
                    next()
                } else {
                    next(false)
                }
            }
        },
        { path: '/paysuccess', component: Paysuccess, meta: { footIsShow: true } },
        {
            path: '/center',
            component: Center,
            redirect: '/center/myorder',
            meta: { footIsShow: true },
            children: [{
                    path: 'myorder',
                    component: Myorder
                },
                {
                    path: 'grouporder',
                    component: Grouporder
                }
            ]
        }
    ],
    // 新页面滚动条直接在顶部
    scrollBehavior(to, from, savedPosition) {
        return { y: 0 }
    }
})

import store from '@/store/index'
// 全局守卫
router.beforeEach(async(to, from, next) => {
    let token = localStorage.getItem('TOKEN')
    let userName = store.state.user.userInfo.name
    if (token) {
        // 如果用户已经登录
        if (to.path == '/login') {
            next('/home')
        } else {
            // 如果没用户名得触发一下getuser函数
            if (userName) {
                next()
            } else {
                await store.dispatch('getUserInfo')
                next()
            }
        }
    } else {
        // 未登录的话，不能去交易、支付、个人中心这些要权限的地方
        if (to.path.indexOf('/pay') != -1 || to.path.indexOf('/center') != -1 || to.path.indexOf('/trade') != -1) {
            next('/login?redirect=' + to.path)
        } else {
            next()
        }
    }
})

export default router